///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_IO_CTEXTWRITER_H_
#define _BUOLA_IO_CTEXTWRITER_H_

#include <buola/io/cbufferstream.h>

namespace buola { namespace io {

class CTextWriter
{
public:
    CTextWriter(PBufferStream pStream,bool pFlushOnNewLine=false)
        :   mStream(std::move(pStream))
        ,   mFlushOnNewLine(pFlushOnNewLine)
    {}
    
    CTextWriter(const CURI &pURI,EMode pMode=EMode::WRITE);

    ~CTextWriter()
    {
        mStream->Flush();
    }
    
    CResult Write(const char *pData,size_t pLen)
    {
        CResult lResult=mStream->Write((const uint8_t*)pData,pLen);
        if(mFlushOnNewLine&&memchr(pData,'\n',pLen))
            mStream->Flush();
        return lResult;
    }

    CTextWriter &operator<<(const std::string &pString)
    {
        Write(pString.data(),pString.length());
        return *this;
    }
    
    CTextWriter &operator<<(const char *pString)
    {
        Write(pString,strlen(pString));
        return *this;
    }
    
    CTextWriter &operator<<(const std::u16string &pString)    {   return operator<<(utf8(pString)); }
    CTextWriter &operator<<(const char16_t *pString)         {   return operator<<(utf8(pString)); }
    CTextWriter &operator<<(const std::wstring &pString)    {   return operator<<(utf8(pString)); }
    CTextWriter &operator<<(const wchar_t *pString)         {   return operator<<(utf8(pString)); }

    CTextWriter &operator<<(char pChar)
    {
        Write(&pChar,1);
        return *this;
    }
    
    CTextWriter &operator<<(char16_t pChar)     {   return operator<<(std::u16string(1,pChar)); }
    CTextWriter &operator<<(wchar_t pChar)      {   return operator<<(std::wstring(1,pChar));   }
    
    CTextWriter &operator<<(uint8_t pInt);
    CTextWriter &operator<<(int8_t pInt);
    CTextWriter &operator<<(uint16_t pInt);
    CTextWriter &operator<<(int16_t pInt);
    CTextWriter &operator<<(uint32_t pInt);
    CTextWriter &operator<<(int32_t pInt);
    CTextWriter &operator<<(uint64_t pInt);
    CTextWriter &operator<<(int64_t pInt);
    
    CTextWriter &operator<<(float pReal)        {   return operator<<(to_string(pReal));    }
    CTextWriter &operator<<(double pReal)       {   return operator<<(to_string(pReal));    }
    CTextWriter &operator<<(long double pReal)  {   return operator<<(to_string(pReal));    }

    CTextWriter &operator<<(bool pBool)     {   return operator<<(to_string(pBool));    }

    CTextWriter &operator<<(NFlush)
    {
        mStream->Flush();
        return *this;
    }

    const PBufferStream &Stream()   {   return mStream; }

private:
    PBufferStream mStream;
    bool mFlushOnNewLine;
};

template<typename tChar,typename tTraits>
inline io::CTextWriter &operator<<(io::CTextWriter &pWriter,const basic_string_view<tChar,tTraits> &pStr)
{
    pWriter.Write(pStr.data(),pStr.length());
    return pWriter;
}

inline io::CTextWriter &operator<<(io::CTextWriter &pWriter,const CURI &pURI)
{
    pWriter << pURI.ToString();
    return pWriter;
}

/*namespace io*/ } /*namespace buola*/ }

#endif
